﻿namespace Widgets.DisqusCommentsSummary
{
    using System.Web.UI;

    using App_Code.Controls;

    using BlogEngine.Core;

    public partial class Widget : WidgetBase
    {
        #region Constants and Fields

        /// <summary>
        /// The container type to show
        /// possible values: 'combo', 'recent', 'popular', 'people'
        /// </summary>
        private string WidgetType = "combo";

        /// <summary>
        /// The number of items to display in the widget
        /// </summary>
        private int NumberOfItems = 5;

        /// <summary>
        /// The tab view to show initially
        /// possible values: 'recent', 'popular', 'people'
        /// </summary>
        private string ComboTabView = "people";

        /// <summary>
        /// The combo widget theme
        /// possible values: 'blue', 'grey', 'green', 'red', 'orange'
        /// </summary>
        private string ComboTheme = "blue";

        /// <summary>
        /// Show avatars?
        /// </summary>
        private bool ShowAvatars = true;

        /// <summary>
        /// Avatar size in pixels
        /// possible values: '24', '32', '48', '92', '128'
        /// </summary>
        private int AvatarSize = 32;

        /// <summary>
        /// The number of characters to show per comment
        /// </summary>
        private int CommentExcerptLength = 200;

        /// <summary>
        /// Should moderators be included in the "top commenters" section?
        /// </summary>
        private bool ShowModeratorsInRanking = true;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes static members of the <see cref = "Widget" /> class.
        /// </summary>
        static Widget()
        {
            BlogSettings.Changed += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_disquscommentssummary");
        }

        #endregion

        #region Properties

        /// <summary>
        ///     Gets a value indicating whether IsEditable.
        /// </summary>
        public override bool IsEditable
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        ///     Gets Name.
        /// </summary>
        public override string Name
        {
            get
            {
                return "DisqusCommentsSummary";
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// This method works as a substitute for Page_Load. You should use this method for
        ///     data binding etc. instead of Page_Load.
        /// </summary>
        public override void LoadWidget()
        {
            if (Blog.CurrentInstance.Cache["widget_disquscommentssummary"] == null)
            {
                string divid = string.Empty;
                string src = string.Empty;

                var settings = this.GetSettings();

                if (settings.ContainsKey("widgettype")) WidgetType = settings["widgettype"];
                if (settings.ContainsKey("numberofitems")) NumberOfItems = int.Parse(settings["numberofitems"]);
                if (settings.ContainsKey("showmoderatorsinranking")) ShowModeratorsInRanking = bool.Parse(settings["showmoderatorsinranking"]);
                if (settings.ContainsKey("combotheme")) ComboTheme = settings["combotheme"];
                if (settings.ContainsKey("combotabview")) ComboTabView = settings["combotabview"];
                if (settings.ContainsKey("commentexcerptlength")) CommentExcerptLength = int.Parse(settings["commentexcerptlength"]);
                if (settings.ContainsKey("showavatars")) ShowAvatars = bool.Parse(settings["showavatars"]);
                if (settings.ContainsKey("avatarsize")) AvatarSize = int.Parse(settings["avatarsize"]);

                switch (WidgetType)
                {
                    case "combo":
                        divid = "disquscombination";
                        src = string.Format("http://{0}.disqus.com/combination_widget.js?num_items={1}&hide_mods={2}&color={3}&default_tab={4}&excerpt_length={5}", BlogSettings.Instance.DisqusWebsiteName, NumberOfItems, ShowModeratorsInRanking ? "0" : "1", ComboTheme, ComboTabView, CommentExcerptLength);
                        break;
                    case "recent":
                        divid = "recentcomments";
                        src = string.Format("http://{0}.disqus.com/recent_comments_widget.js?num_items={1}&hide_avatars={2}&avatar_size={3}&excerpt_length={4}", BlogSettings.Instance.DisqusWebsiteName, NumberOfItems, ShowAvatars ? "0" : "1", AvatarSize, CommentExcerptLength);
                        break;
                    case "popular":
                        divid = "popularthreads";
                        src = string.Format("http://{0}.disqus.com/popular_threads_widget.js?num_items={1}", BlogSettings.Instance.DisqusWebsiteName, NumberOfItems);
                        break;
                    case "people":
                        divid = "topcommenters";
                        src = string.Format("http://{0}.disqus.com/top_commenters_widget.js?num_items={1}&hide_mods={2}&hide_avatars={3}&avatar_size={4}", BlogSettings.Instance.DisqusWebsiteName, NumberOfItems, ShowModeratorsInRanking ? "0" : "1", ShowAvatars ? "0" : "1", AvatarSize);
                        break;
                    default:

                        return;
                }

                string script = string.Format("<div id=\"{0}\" class=\"dsq-widget\">", divid);
                script += string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", src);
                script += "</div>";

                Blog.CurrentInstance.Cache.Insert("widget_disquscommentssummary", script);
            }

            string content = (string)Blog.CurrentInstance.Cache["widget_disquscommentssummary"];

            var html = new LiteralControl(content);

            this.phDisqusCommentsSummary.Controls.Add(html);
        }

        #endregion
    }
}